package com.frxuexi.user.view

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.frxuexi.lib_base.IUiState
import com.frxuexi.lib_base.MyTitleBar
import com.frxuexi.lib_base.room.AskLeave
import com.frxuexi.user.intent.AskIntent
import com.frxuexi.user.viewmodel.AskViewModel
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

/**
 * 审批页面
 */
@Composable
fun ApprovePage(navHostController: NavHostController,json:String,vm:AskViewModel= hiltViewModel()) {


    val ask = Gson().fromJson(json, AskLeave::class.java)

    val kv = MMKV.defaultMMKV()
    val uname by remember {
        mutableStateOf(kv.decodeString("uname", "未登录").toString())
    }

    val role by remember {
        mutableStateOf(kv.decodeInt("role", 0))
    }

    var status by remember {
        mutableStateOf("同意")
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.uiState.collect{
                when(it){
                    is IUiState.onSuccess<*> ->{
                        navHostController.popBackStack()
                    }else->{}
                }
            }
        }
    })


    Column {
        MyTitleBar(navHostController = navHostController, title = "请假详情")
        Spacer(modifier = Modifier.height(10.dp))
        Column(Modifier.padding(20.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
            Text(text = "申报人信息", color = Color.Blue)
            Card(
                colors = CardDefaults.cardColors(containerColor = Color.White),
                border = BorderStroke(1.dp, Color.LightGray)
            ) {
                Row(
                    Modifier
                        .fillMaxWidth()
                        .padding(20.dp)
                ) {
                    AsyncImage(
                        model = "https://img0.baidu.com/it/u=3465811579,2310782493&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1727629200&t=a4a813928f902aff74268863728d556b",
                        contentDescription = "",
                        modifier = Modifier
                            .size(80.dp)
                            .clip(CircleShape),
                        contentScale = ContentScale.Crop
                    )
                    Spacer(modifier = Modifier.width(10.dp))
                    Column(Modifier.height(80.dp), verticalArrangement = Arrangement.SpaceAround) {
                        Text(text = uname)
                        Text(text = if (role == 1) "经理" else if (role == 2) "员工" else "")
                    }
                }
            }
            Spacer(modifier = Modifier.height(20.dp))
            Text(text = "请假详情", color = Color.Blue)
            Text(text = "请假方式：${ask.type}")
            Text(text = "开始时间：${ask.start}")
            Text(text = "结束时间：${ask.end}")
            Text(text = "请假原因：${ask.reason}")

            Spacer(modifier = Modifier.height(20.dp))
            Text(text = "审批意见", color = Color.Blue)
            Divider()
            Row(horizontalArrangement = Arrangement.spacedBy(10.dp), verticalAlignment = Alignment.CenterVertically) {
                Text(text = "意见内容：")
                RadioButton(selected = status=="同意", onClick = {
                    status="同意"
                })
                Text(text = "同意")
                RadioButton(selected = status=="拒绝", onClick = {
                    status="拒绝"
                })
                Text(text = "拒绝")
            }

            Button(onClick = {
                            ask.status=status
                val time =
                    SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())
                ask.comTime=time
                vm.sendIntent(AskIntent.approve(askLeave = ask))
            }, modifier = Modifier.fillMaxWidth()) {
                Text(text = "提交")
            }
        }
    }
}